package com.dunke.html.impl;

import com.dunke.core.common.Result;
import com.dunke.core.common.ResultPage;
import com.dunke.core.common.TokenData;
import com.dunke.core.dao.AdminDao;
import com.dunke.core.dao.AdminLogDao;
import com.dunke.core.data.GetData;
import com.dunke.core.data.IpAddrData;
import com.dunke.core.data.PageData;
import com.dunke.core.data.admin.*;
import com.dunke.core.dto.AdminDto;
import com.dunke.core.dto.AdminLogDto;
import com.dunke.core.dto.LoginDto;
import com.dunke.core.enums.ReturnCode;
import com.dunke.core.html.User;
import com.dunke.html.LoginService;
import com.dunke.service.AdminService;
import com.dunke.utils.DateUtils;
import com.dunke.utils.IpServletUtils;
import com.dunke.utils.JedisUtil;
import com.dunke.utils.TokenUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * (DbAdmin)表服务实现类
 *
 * @author makejava
 * @since 2021-06-23 18:22:53
 */
@Service("LoginService")
public class LoginServiceImpl implements LoginService {

    @Resource
    @Autowired
    private AdminDao adminDao;

    @Resource
    @Autowired
    private AdminLogDao logDao;

    @Autowired
    TokenUtil tokenUtil;

    @Override
    public TokenData login(User user) {
        AdminDto adminDto = this.adminDao.login(user.getAccount());
        if (adminDto == null) {
            return null;
        } else if (!user.getPassword().equals(adminDto.getPassword())) {
            return null;
        }
        TokenData tokenData = new TokenData(adminDto.getId(),
                adminDto.getAccount(), adminDto.getPassword(), DateUtils.getCurrentMillis());
        // 生成Token
        String token = tokenUtil.getToken(tokenData);
        adminDto.setToken(token);
        adminDto.setPassword("");
        tokenData.setToken(token);
        JedisUtil jedisUtil = new JedisUtil();
        // 存储Token
        jedisUtil.setToken(tokenData);
        return tokenData;
    }
}
